package b1.net
{
	import b1.players.*;
	import b1.scenes.*;
	import b1.utils.*;
	
	import flash.events.*;
	import flash.net.*;
	
	//Ren Ren API
	public class RRApiHelper
	{
		//回调函数
		private var callBackFunc:Function;
		 
		public static function generateSig(variables:URLVariables, secret:String):String
		{
			var varArray:Array = new Array();
			for (var key:String in variables)
			{
				varArray.push(key + "=" + variables[key]);
			} 
			varArray.sort();
			var varStr:String = "";
			for (var i:int = 0; i < varArray.length; i++)
			{
				varStr += varArray[i];
			}
			varStr += secret;
			return Md5.hash(varStr);
		} 

		public function getUserPic(rrUserId:int, callBackFunc:Function = null):void
		{
			SceneContainer.getInstance().debug("RRApiHelper.getUserPic");
			this.callBackFunc = callBackFunc;
			var variables:URLVariables = new URLVariables();
			variables.api_key = Profile.rrApiKey;
			variables.method = "users.getInfo";
			variables.v = Profile.rrApiVersion;
			variables.uids = rrUserId;
			var currentDate:Date = new Date();
			variables.call_id = currentDate.getTime();
			variables.sig = generateSig(variables, Profile.rrSecret);
			var request:URLRequest = new URLRequest(Profile.rrRestServer);
			request.method = URLRequestMethod.POST;
			request.data = variables;
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, handleGetUserPic);
			loader.load(request);
		}
		
		private function handleGetUserPic(event:Event):void
		{
			SceneContainer.getInstance().debug("RRApiHelper.handleGetUserPic");
			var dataXML:XML = XML(event.target.data);
			var rrTinyUrl:String = dataXML..*::tinyurl[0].text();
			var rrUserName:String = dataXML..*::name[0].text();
//			getFriends(PlayerInfoHelper.getInstance().rrUserId, callBackFunc);
			if (callBackFunc != null)
				callBackFunc(rrTinyUrl, rrUserName);
		}
		
		public function getFriends(rrUserId:int, callBackFunc:Function = null):void
		{
			SceneContainer.getInstance().debug("RRApiHelper.getFriends");
			this.callBackFunc = callBackFunc;
			var variables:URLVariables = new URLVariables();
			variables.api_key = Profile.rrApiKey;
			variables.method = "friends.getAppFriends";
			variables.session_key = Profile.rrSessionKey;
			variables.v = Profile.rrApiVersion;
			variables.uids = rrUserId;
			variables.fields = "name,tinyurl";
			var currentDate:Date = new Date();
			variables.call_id = currentDate.getTime();
			variables.sig = generateSig(variables, Profile.rrSecret);
			var request:URLRequest = new URLRequest(Profile.rrRestServer);
			request.method = URLRequestMethod.POST;
			request.data = variables;
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, completeFriends);
			loader.load(request);
		}

		private function completeFriends(event:Event):void
		{
			var dataXML:XML = XML(event.target.data);
			SceneContainer.getInstance().debug("RRApiHelper.completeFriends");
			var len:int = dataXML..*::name.length();
			var rrPlayerIds:Array = new Array();
			for (var i:int = 0; i < len; i++)
			{
				var rrUserId:int = parseInt(dataXML..*::uid[i].text());
				rrPlayerIds.push(rrUserId);
			}
			ServerHelper.getInstance().getRrFriends(rrPlayerIds, callBackFunc);
//			if (callBackFunc != null)
//				callBackFunc(rrPlayerIds);
		}
		
	}
}